package com.seven.restaurant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seven.restaurant.entity.po.OrderItemPO;
import com.seven.restaurant.entity.request.OrderRequest;
import com.seven.restaurant.entity.vo.OrderDetailsVO;
import com.seven.restaurant.exception.ValidationException;
import com.seven.restaurant.service.OrderItemService;
import com.seven.restaurant.service.OrderService;
import com.seven.restaurant.util.ResponseEntity;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author BaiBan
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService service;

    @Autowired
    OrderItemService orderItemService;

    /**
     * 菜单列表
     * @param current 页数
     */
    @GetMapping("/list")
    public ResponseEntity<IPage<OrderDetailsVO>> list(@RequestParam(value = "current", defaultValue = "1")Integer current) {
        return ResponseEntity.success(service.list(current));
    }

    /**
     * 获取一个订单的所有内容
     * @param id 订单编号
     * @return {@link OrderDetailsVO}
     */
    @ApiOperation(value ="详情", notes = "获取一个订单的所有内容")
    @ApiImplicitParam(name="id", value="订单编号", paramType="query")
    @ApiResponse(code=200, message="请求处理成功", response = OrderDetailsVO.class)
    @GetMapping("/details")
    public ResponseEntity<OrderDetailsVO> details(Long id) {
        return ResponseEntity.success(service.get(id));
    }


    @ApiOperation(value ="添加订单", notes = "添加一个订单及其内容")
    @ApiResponses({
            @ApiResponse(code=220, message="数据创建成功"),
            @ApiResponse(code=420, message="数据创建失败"),
    })
    @PostMapping("/create")
    public ResponseEntity<?> create(@RequestBody OrderRequest OrderRequest) throws ValidationException {
        // 添加订单
        Long id = service.save(OrderRequest);

        // 添加订单项
        for (OrderItemPO item : OrderRequest.getOrderItems()) {
            item.setOrderId(id);
            orderItemService.save(item);
        }
        return ResponseEntity.create(true);
    }


    /**
     * 删除订单
     * @param id 订单id
     * @return 删除结果
     */
    @ApiOperation(value ="删除订单", notes = "根据订单id删除菜品")
    @ApiImplicitParam(name="id", value="订单编号", required=true, paramType="query")
    @ApiResponses({
            @ApiResponse(code=225, message="数据删除成功"),
            @ApiResponse(code=425, message="数据删除失败"),
    })
    @PostMapping("/delete")
    public ResponseEntity<?> delete(@RequestBody Long id) {
        return ResponseEntity.delete(service.removeById(id));
    }


    @ApiOperation(value = "订单上菜完成", notes = "修改订单的上菜标记，完成上菜")
    @ApiImplicitParam(name = "id", value = "订单编号", dataType = "Long")
    @ApiResponses({
            @ApiResponse(code=230, message="数据更新成功"),
            @ApiResponse(code=430, message="数据更新失败"),
    })
    @PostMapping("/update/serve")
    public ResponseEntity<?> updateServe(@RequestBody Long id) {
        return ResponseEntity.update(service.update(id, "serve"));
    }


    @ApiOperation(value = "订单支付完成", notes = "修改订单的支付标记，顾客完成支付")
    @ApiImplicitParam(name = "id", value = "订单编号", dataType = "Long")
    @ApiResponses({
            @ApiResponse(code=230, message="数据更新成功"),
            @ApiResponse(code=430, message="数据更新失败"),
    })
    @PostMapping("/update/pay")
    public ResponseEntity<?> updatePay(@RequestBody Long id) {
        return ResponseEntity.update(service.update(id, "pay"));
    }
}